package com.vue.api.service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vue.api.dao.LoginMapper;
import com.vue.api.entity.Menu;
import com.vue.api.entity.Page;
import com.vue.api.entity.PageResult;
import com.vue.api.entity.Role;
import com.vue.api.entity.User;

@Service
public class LoginService {

	@Autowired
	private LoginMapper loginMapper;

	public User checkUser(User user) {
		// TODO Auto-generated method stub
		return loginMapper.findByUserName(user.getUserName());
	}

	/**
	 * 递归查找子菜单
	 * id 当前菜单id
	 * menuList  所有的菜单
	 */
	private List<Menu> getChild(String id, List<Menu> menuList) {
		// 找二级子菜单
		List<Menu> childList = new ArrayList<>();
		for (Menu menu : menuList) {
			// 遍历所有菜单，将父菜单id与传过来的id比较，如果相等就是该父菜单下的子菜单
			if (menu.getPid() .equals(id)) {
				childList.add(menu);
			}
		}
		// 把子菜单的子菜单再循查找子菜单
		for (Menu menu : childList) {
			List<Menu> child = getChild(menu.getId(), menuList);
			menu.setChildList(child);
		}
		return childList;
	}


	public List<Menu> getAllMenu() {
		// TODO Auto-generated method stub
		List<Menu> allList = loginMapper.getAllMenu();
		List<Menu> respList = new ArrayList<>();  // respList 装最后返回的结果
		// 先找到所有的一级菜单
		for (Menu value : allList) {
			//一级菜单parentId=0；
			if (value.getPid().equals("0")) {
				respList.add(value);
			}
		}

		// 遍历一级菜单，开始找子菜单
		for (Menu menu : respList) {
			List<Menu> child = getChild(menu.getId(), allList);
			menu.setChildList(child);
		}
		return respList;
	}


	public Page<User> findByPage(Page<User> page, User user) {	
		page.setObj(user);
		List<User> userList=loginMapper.findUserByPage(page);
		page.setResult(userList);
		int  count=loginMapper.findCountByUser();
		page.setTotalCount(count);
		return page;
	}

	public int updatUserState(User user) {
		// TODO Auto-generated method stub
		return loginMapper.updatUserState(user);
	}

	public int addUser(User user) {
		// TODO Auto-generated method stub
		String id=UUID.randomUUID().toString().replaceAll("-", "");
		user.setId(id);
		user.setState(1);
		return loginMapper.addUser(user);
	}

	public User findUserById(String id) {
		// TODO Auto-generated method stub
		return loginMapper.findUserById(id);
	}

	public List<Role> getAllRoleMenu() {
		// TODO Auto-generated method stub
		List<Role> roleList=loginMapper.getAllRoleMenu();
		for (Role role : roleList) {
			List<Menu> roleMenu = getRoleMenu(role.getId());
			role.setMenuList(roleMenu);
		}
		return roleList;
	}
	
	public List<Menu> getRoleMenu(String roleId) {
		// TODO Auto-generated method stub
		List<Menu> allList = loginMapper.getRoleMenu(roleId);
		List<Menu> respList = new ArrayList<>();  // respList 装最后返回的结果
		// 先找到所有的一级菜单
		for (Menu value : allList) {
			//一级菜单parentId=0；
			if (value.getPid().equals("0")) {
				respList.add(value);
			}
		}

		// 遍历一级菜单，开始找子菜单
		for (Menu menu : respList) {
			List<Menu> child = getChild(menu.getId(), allList);
			menu.setChildList(child);
		}
		return respList;
	}

	public int updateUser(User user) {
		// TODO Auto-generated method stub
		return loginMapper.updateUser(user);
	}

	public int deleteUser(String id) {
		// TODO Auto-generated method stub
		return loginMapper.deleteUser(id);
	}

}
